<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Search extends CI_Controller {
	
	private $pageSize = 20;
	
	/**
	 * 处理用户登录
	 * @param unknown $email
	 */
	public function index()
	{
		$search = $this->input->get('r');
		if(!$search)
		{
			redirect(base_url());
		}
		
		$this->load->model("m_books");
		$this->load->model("m_search");
		
		$data = array( 'style' => 'search/index' ,'title'=>$search."_作业本教材搜索");
		
		//商品的数量
		$currentPage = $this->input->get('p')?$this->input->get('p'):'1';
		
		//输入isbn号
		//查询isbn是否有结果，如果没有，则检索书本名称
		//先看看是不是数字
		$books = array();
		
		if(is_numeric($search))
		{
			//猜测这个是isbn号
			if(substr($search, 0, 3) == '978' || substr($search, 0, 1) == '7')
			{
				$bookAmount = $this->m_books->searchBookAmountByIsbn($search);
				$bookAmount = $bookAmount['0']->bookAmount?$bookAmount['0']->bookAmount:'0';
				$books = $this->m_books->searchBookByIsbn($search , ($currentPage - 1)* $this->pageSize , $this->pageSize);
			}
			else 
			{
				//这里是数字但是似乎不是isbn，则isbn和书本名称都要检索一下
				$bookAmount = $this->m_books->searchBookAmountByIsbn($search);
				$bookAmount = $bookAmount['0']->bookAmount?$bookAmount['0']->bookAmount:'0';
				$books = $this->m_books->searchByNameIsbn($search , ($currentPage - 1)* $this->pageSize , $this->pageSize);
			}

		}
		else 
		{
			//如果这个玩意不是纯数字,需要检索书本名称，书本作者，出版社，分类
			//先看看有没有分类匹配的
			$this->load->model("m_category");
			$category = $this->m_category->searchCategoryByName($search);
			
			$bookAmount = $this->m_books->searchBooksAmountByAll($search , $category);
			$bookAmount = $bookAmount['0']->bookAmount?$bookAmount['0']->bookAmount:'0';
			$books = $this->m_books->searchBooksByAll($search , ($currentPage - 1)* $this->pageSize , $this->pageSize ,$category);
			
		}
		
		$pagination = array('pageSize'=>$this->pageSize,'totalRows'=>$bookAmount,'currentPage'=>$currentPage,'baseUrl'=>base_url('search?r='.$search));
		$this->load->library('pagination',$pagination);
		
		$this->m_search->insertSearch($search ,$bookAmount);
		
		$data['searchBooks'] = $books;
		
		$this->load->view('header',$data);
		$this->load->view('search/index');
		$this->load->view('footer');
	}

}

/* End of file Register.php */
/* Location: ./application/controllers/Register.php */